package com.shop.dao.imp;

import com.shop.beans.Address;
import com.shop.beans.User;
import com.shop.dao.AddressDao;
import com.shop.dao.UserDao;
import com.shop.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class AddressDaoImp01 implements AddressDao {
    @Override
    public void addAddress(Address address) {
        //boolean flag = false;
        UserDao userDao = new UserDaoImp01();
        User user=userDao.getUserById(address.getUser_id());
        if(user==null){
            System.out.println("【INFO】=>ADD-[FAL]添加地址失败，无法找到id为"+
                    address.getUser_id()+"的用户");
                    return;
        }
        try {
            Connection connection=new JDBCUtil().getConnection();
            String sql="insert into t_address values(null,?,?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,address.getUser_id());
            ps.setString(2,address.getAddress());
            ps.setInt(3,address.getPostalcode());
            ps.setString(4,address.getConname());
            ps.setString(5,address.getConphone());
            ps.execute();
            System.out.println("【INFO】=>ADD-[ACC]添加地址成功用户名为"+user.getUsername()+"\n\t\t->|"+address);
            connection.close();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAddress(Address address) {
        try {
            int id=address.getId();
            UserDao userDao = new UserDaoImp01();
            User user=userDao.getUserById(address.getUser_id());
            if(user==null){
                System.out.println("【INFO】=>UP-[FAL]修改地址失败，无法找到id为"+
                        address.getUser_id()+"的用户");
                return;
            }
            Connection connection= JDBCUtil.getConnection();
            String sql="update t_address set  user_id=? , address=? , postalcode=? , conname=? , conphone=?  where id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setInt(1,address.getUser_id());
            ps.setString(2,address.getAddress());
            ps.setInt(3,address.getPostalcode());
            ps.setString(4,address.getConname());
            ps.setString(5,address.getConphone());
            ps.setInt(6,id);
            ps.execute();
            System.out.println("【INFO】=>UP-[ACC]修改id为"+id+"用户id为"+address.getUser_id()+"的地址成功\n\t\t->|"+address);
            connection.close();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAddress(int id) {
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="delete  from t_address where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.execute();
            System.out.println("【INFO】=>DEL-[ACC]删除id为"+id+"地址为"+"的地址成功\n\t\t->|");
            connection.close();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Address getAddressById(int id) {
        Address address1=new Address();
        try{
            Connection connection=JDBCUtil.getConnection();
            String sql="select * from t_address where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet resultSet=ps.executeQuery();
            while (resultSet.next()){
                int user_id=resultSet.getInt("user_id");
                String address=resultSet.getString("address");
                int postalcode=resultSet.getInt("postalcode");
                String conname=resultSet.getString("conname");
                String conphone=resultSet.getString("conphone");
                Address address2=new Address(id,user_id,address,postalcode,conname,conphone);
                address1=address2;
                connection.close();
                break;
            }
            if ((address1.getAddress()==null)&&(address1.getConname()==null)&&address1.getConphone()==null){
                System.out.println("【INFO】=>GetAddress-[FAL]获取地址id为"+id+"失败");
                return null;
            }
            System.out.println("【INFO】=>GetUsers-[ACC]获取用户成功\n\t\t->|"+address1);
            return address1;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Address> getAddressesByUser(int user_id) {
        List<Address>addresses=new ArrayList<>();
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="select * from t_address where user_id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,user_id);
            ResultSet resultSet= ps.executeQuery();
            //ResultSet resultSet= executeQuery(sql);
            System.out.println("【INFO】=>GetAddresss-[ACC]获取地址成功用户id为"+user_id);
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                int user_id_t=resultSet.getInt("user_id");
                String address=resultSet.getString("address");
                int postalcode=resultSet.getInt("postalcode");
                String conname=resultSet.getString("conname");
                String conphone=resultSet.getString("conphone");
                Address address1=new Address(id,user_id_t,address,postalcode,conname,conphone);
                System.out.println("\t\t->|"+address1);
                addresses.add(address1);
            }
            System.out.println("\t\t->|共计"+addresses.size());
            connection.close();
            return addresses;
            //ps.execute();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
